<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img
          src="../assets/heima.png"
          alt=""
        >
        <div class="logo_words">黑马后台管理系统</div>
      </div>
      <!-- 退出按钮 -->
      <el-button
        type="info"
        @click="exitPage"
      >退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <el-aside
        :width="isCollapse ? '64px' : '200px'"
      >

        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <div class="toggle-button" @click="collapseTab"> 三 </div>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单内部信息 -->
            <template slot="title">
              <i :class="iconSys[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 对应的选项 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="keppActive('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconSys: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-morentouxiang',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: true,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    exitPage() {
      window.sessionStorage.clear()
      setTimeout(() => {
        this.$router.push('/login')
      }, 1500)
    },
    async getMenuList() {
      let { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // console.log(res)
      this.menuList = res.data
    },
    collapseTab() {
      this.isCollapse = !this.isCollapse
    },
    keppActive(path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-header {
  background-color: #333;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  > div {
    display: flex;
    flex-direction: row;
  }
  .logo_words {
    font-size: 24px;
    color: #fff;
    padding-left: 20px;
    margin-top: 10px;
  }
}
.el-aside {
  background-color: #303134;
  height: 100%;
  transition: width .1s ease-in;
  /* css 禁止选中文字 */
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Chrome/Safari/Opera */
  -khtml-user-select: none; /* Konqueror */
  -moz-user-select: none; /* Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
  user-select: none;
  .toggle-button {
    text-align: center;
    color: #fff;
    height: 30px;
    line-height: 30px;
    background-color: #444;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
    .iconfont {
      margin-right: 10px;
    }
  }
}
.el-main {
  background-color: #e4e4e4;
  height: 100%;
}
</style>